package com.qdev.apps.core.webwork.validator;

import com.opensymphony.xwork.validator.ValidationException;
import com.opensymphony.xwork.validator.validators.FieldValidatorSupport;

/**
 * @author Quake Wang
 * @since 2005-1-10
 * @version $Revision: 1.2 $
 */
public class NumberFieldValidator extends FieldValidatorSupport {
    private Double min;
    private Double max;
    
    public void validate(Object object) throws ValidationException {
        String fieldName = getFieldName();
        Object value = this.getFieldValue(fieldName, object);
        // if there is no value - don't do comparison
        // if a value is required, a required validator should be added to the
        // field
        if (value == null)
            return;
        if (!(value instanceof Number)) {
            addFieldError(fieldName, object);
            return;
        }
        if ((min != null) && (((Number)value).doubleValue() <= min.doubleValue())) {
            addFieldError(fieldName, object);
            return;
        }
    }

    //getter and setter 
    public Double getMax() {
        return max;
    }
    public void setMax(Double max) {
        this.max = max;
    }
    public Double getMin() {
        return min;
    }
    public void setMin(Double min) {
        this.min = min;
    }
}